/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wetest;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import we.common.CDebug;
import we.config.CConstant;
import we.dispatcher.CPacketReader;
import we.dispatcher.packet.CEchoSentPacket;
import we.dispatcher.packet.CSentPacket;
import we.exception.ReadingPacketException;
import we.exception.UnsupportedTypeException;

/**
 *
 * @author ADMIN
 */
public class WeClient {

    class ReadThread extends Thread {

        @Override
        public void run() {
            CPacketReader reader = new CPacketReader(1024);
            //ignore first 87 bytes - handshake;
            long count = 0;
            try {
                while (true) {                    
                    byte b = (byte) in.read();
                    
                    count++;
                    if (count <= 87)
                        continue;
                    System.out.println("read : " + b);
                    // socket.close();
                    reader.put(b);
                    if (reader.checkCompletedPackage()) {
                        System.out.println("--------reader complete");
                        try {
                            int id = reader.readID();
                            CDebug.Trace(this, "read ID : " + id);
                            if (id == 1000) {
                                String s = (String) reader.read(String.class)[0];
                                System.out.println("read : " + s);
                            }
                        } catch (ReadingPacketException ex) {
                            Logger.getLogger(WeTest.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (UnsupportedTypeException ex) {
                            Logger.getLogger(WeTest.class.getName()).log(Level.SEVERE, null, ex);
                        }

                        reader.clearAll();
                    }
                }
            } catch (ReadingPacketException ex) {
                Logger.getLogger(WeClient.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(WeTest.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    InputStream in;
    OutputStream out;
    Socket socket;

    public void send(CSentPacket packet) throws IOException {
        ByteBuffer buff = packet.getBuffer();
        CDebug.Trace(this, "length " + buff.remaining());
        StringBuilder string = new StringBuilder();
        while (buff.hasRemaining()) {
            byte b = buff.get();
            string.append(b + ", ");
            out.write(b);
        }
        CDebug.Trace(this, string);
        out.flush();
    }

    public void run() throws Exception {
        Scanner input = new Scanner(System.in);
        socket = new Socket("localhost", CConstant.SERVER_PORT);
        in = socket.getInputStream();
        out = socket.getOutputStream();

        new ReadThread().start();
        
        int id = 1;
        while (id != 0) {
            id = input.nextInt();
            CDebug.Trace(this, " cmd ID : " + id);
            switch (id) {
                case 0:
                    System.exit(0);
                    return;
                    
                case 1:
                    CEchoSentPacket sent = new CEchoSentPacket();
                    sent.echo = "abc";
                    sent.processData();
                    if (sent.isValid()) {
                        send(sent);
                    }
                    break;

                case 2:
                    LoginSentPacket packet = new LoginSentPacket();
                    packet.processData();
                    send(packet);
//                    ByteBuffer buff = packet.getBuffer();
//                    while (buff.hasRemaining()) {
//                        byte tmp = buff.get();
//                        Debug.trace("out : " + tmp);
//                        out.write(tmp);
//                    }
//                    out.flush();
                    break;
            }
        }
    }

    public static void main(String[] args) throws Exception {
        new WeClient().run();
    }
}
